package Java.BlackHorse.Thread.Lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread extends Thread{
    public MyThread() {
    }

    public MyThread(String name) {
        super(name);
    }

    static int ticket = 0;
    static Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while(true){
            try {
                lock.lock();
                if(ticket >= 100){
                    break;
                }else{
                    ticket++;
                    System.out.println(Thread.currentThread().getName()+" 在抢第 "+ticket+" 张票 ");
                    sleep(100);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }finally{
                lock.unlock();
            }
        }
    }
}
